package com.dearbinge.opentapi.content.moneyDetail;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.moneyDetail.MoneyDetailService;
import com.dearbinge.data.pojo.moneyDetail.MoneyDetail;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

@Api(tags = "c：红包明细")
@CrossOrigin
@RestController
@RequestMapping("/moneyDetail")
@EnableAutoConfiguration
public class MoneyDetailController {

    @Autowired
    private MoneyDetailService moneyDetailService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody MoneyDetail moneyDetail){
        try {
            if (moneyDetail.getId() != null && moneyDetail.getId() != 0){
                Integer dataCode = moneyDetailService.update(moneyDetail);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Integer dataCode = moneyDetailService.insert(moneyDetail);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID逻辑删除
     */
    @ApiOperation(value = "根据ID逻辑删除",notes = "根据ID逻辑删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = moneyDetailService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据条件分页查询
     */
    @ApiOperation(value = "根据条件分页查询",notes = "根据条件分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
                        @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
                        @ApiImplicitParam(name = "pageSize",value = "页面数量",paramType = "query"),
                        @ApiImplicitParam(name = "isDeleted",value = "是否删除（0：不删除   1：删除）",paramType = "query")
                    })
    @RequestMapping(value = "/getByPage",method = RequestMethod.GET)
    public ResultMsg<Page> getByPage(String userId, Integer pageNo, Integer pageSize,Integer isDeleted){
        try {
            Page page = moneyDetailService.getByPage(userId,pageNo,pageSize,isDeleted);
            return ResultMsg.okData(page,"200");
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
